<template>
	<view class="tab">
		<!-- 滑动tab -->
		<scroll-view scroll-x="true" class="tab-scroll">
			<view class="tab-scroll_box">
				<view class="tab-scroll_item" :class="{active:activeIndex === index}" v-for="(item,index) in list"
					:key="index" @click="clickTab(item,index)">
					{{item.name}}
				</view>
			</view>
		</scroll-view>
		<!-- 设置按钮 -->
		<view class="tab-icons">
			<uni-icons type="gear" size="26" color="#666"></uni-icons>
		</view>
	</view>
</template>

<script>
	export default {
		name: "tab",
		props: {
			// tabList列表数据
			list: {
				type: Array,
				default () {
					return [];
				}
			},
			// tab的当前index
			tabIndex: {
				type: Number,
				default () {
					return 0
				}
			}
		},
		// 可以监听 data 或者props值的变化
		watch: {
			// 
			tabIndex(newVal, oldVal) {
				// console.log(newVal, oldVal);
				this.activeIndex = newVal;
			}
		},
		data() {
			return {
				activeIndex: 0
			};
		},
		methods: {
			clickTab(item, index) {
				// console.log(item, index);
				this.activeIndex = index;
				// 点击上面tab，发送tab事件
				this.$emit('tab', {
					data: item,
					index
				})
			}
		}
	}
</script>

<style lang="scss">
	.tab {
		display: flex;
		width: 100%;
		border-bottom: 1px #f5f5f5 solid;
		background-color: #fff;
		box-sizing: border-box;

		.tab-scroll {
			flex: 1;
			overflow: hidden;
			box-sizing: border-box;
			// border: 1px red solid;

			.tab-scroll_box {
				display: flex;
				height: 45px;
				align-items: center; //垂直居中
				flex-wrap: nowrap; // 不换行
				box-sizing: border-box;

				.tab-scroll_item {
					flex-shrink: 0;
					padding: 0 10px;
					color: #333;
					font-size: 14px;

					&.active {
						color: $mk-base-color;
					}
				}
			}
		}

		.tab-icons {
			position: relative;
			display: flex;
			justify-content: center; //水平方向居中
			align-items: center;
			width: 45px;

			&::after {
				content: '';
				position: absolute;
				top: 12px;
				bottom: 12px;
				left: 1px;
				width: 1px;
				background-color: #ddd;
			}
		}
	}
</style>
